/**
 * Created by j on 2017-05-06.
 */
(function () {
    var submit = document.getElementById('submit');
    var defaultEle = {};
    function getAnswer(arr) {
        var answer = '';
        if (arr.length > 1) {
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].checked) {
                    answer = arr[i].value;
                    break;
                }
            }
        } else {
            answer = arr[0].value;
        }
        return answer;
    }
    function getAllAnswer() {
        var curData = [];
        for (var i = 1; i <= 5; i++) {
            var questionArr = document.getElementsByName('question' + i);
            curData.push(getAnswer(questionArr));
        }
        return curData;
    }
    submit.addEventListener('click', function () {
        var layer = $('#layer');
        layer.addClass(' fadeIn  animated');
        var data = getAllAnswer();
        defaultEle = {
            send_frquency: data[0] ? data[0] : '1',
            send_date: data[1] ? data[1] : '周一',
            send_time: data[2] ? data[2] : '7:00-8:00',
            increased_Fans: data[3] ? data[3] : '1500',
            total_Fans: data[4] ? data[4] : '15000'
        };
        var that = this;
        $.ajax({
            url: '/fht-lite/research',
            type: 'post',
            async: false,
            data: defaultEle,
            dataType: 'json',
            success: function (result) {
                if (result) {
                    that.setAttribute('disabled', 'true');                
                    localStorage.setItem('resultData', JSON.stringify(result));
                    setTimeout(function () {
                        location.hash = "#statistics";
                    }, 1000);
                }
            },
            error: function () {
                alert('提交失败！请重试');
                that.removeAttribute('disabled');
            }
        });
    });
})();